package com.mahjong.server.protocolHandler.game;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.mahjong.server.common.Common;
import com.mahjong.server.mina.protocol.AbsMessageProtocol;
import com.mahjong.server.mina.protocol.DataBuf;
import com.mahjong.server.mina.protocol.MessageProtocol;
import com.mahjong.server.model.Player;
import com.mahjong.server.model.Room;

/**
 * 快速加入房间
 * 
 * @author Simple
 * @date 2013-4-10 上午12:06:28
 * @Description TODO
 */
@Component
@Scope("prototype")
public class QuickEnterRoomReq extends MessageProtocol {

	private static final byte TAG = Common.Req;

	private static final short PROTOCOL_NUM = 0x0009;

	private Logger log = Logger.getLogger(this.getClass());

	@Override
	public short getProtocolNum() {
		return PROTOCOL_NUM;
	}

	@Override
	public byte getTag() {
		return TAG;
	}

	@Override
	public AbsMessageProtocol execute(IoSession session, AbsMessageProtocol req) {
		log.debug(this.getClass().getSimpleName() + " execute");
		Player player = onlineManager.getPlayerBySession(session);
		if (null != player) {
			Room room = roomManager.quickEnterRoom(player);
			if (null != room) {
				return new QuickEnterRoomResp(Common.SUCCESS, room);
			}
		}
		return new QuickEnterRoomResp(Common.FAILD, null);
	}

	@Override
	public void reqDecode(DataBuf buf) {
		log.debug(this.getClass().getSimpleName() + " decode");
	}
}
